﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace YFIN.View.NewsAndCourse
{
    public partial class MediaPlayerNew : UserControl
    {
        public bool IsLeftButtonPressed = false;
        public bool IsLoop = false;  
        public MediaPlayerNew()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(TstringMovieControl_Loaded);  
        }
        void TstringMovieControl_Loaded(object sender, RoutedEventArgs e)   
        {   
            InitializeEvent();   
        }   
  
        private void InitializeEvent()   
        {   
            tbInfo.Text = string.Empty;   
            VolumeSlider.Opacity = 0;   
            VolumeSlider.MouseEnter += new MouseEventHandler(VolumeSlider_MouseEnter);   
            VolumeSlider.MouseLeave += new MouseEventHandler(VolumeSlider_MouseLeave);   
  
            TstringMovieObject.MediaOpened += new RoutedEventHandler(TstringMovieObject_MediaOpened);   
            TstringMovieObject.DownloadProgressChanged += new RoutedEventHandler(TstringMovieObject_DownloadProgressChanged);   
            TstringMovieObject.MarkerReached += new TimelineMarkerRoutedEventHandler(TstringMovieObject_MarkerReached);   
            TstringMovieObject.MediaEnded += new RoutedEventHandler(TstringMovieObject_MediaEnded);   
            TstringMovieObject.CurrentStateChanged += new RoutedEventHandler(TstringMovieObject_CurrentStateChanged);   
  
  
            ControlPlay.MouseLeftButtonDown += new MouseButtonEventHandler(ControlPlay_MouseLeftButtonDown);   
            tMutedButton.MouseLeftButtonDown += new MouseButtonEventHandler(tMutedButton_MouseLeftButtonDown);   
            tPlayButton.MouseLeftButtonDown += new MouseButtonEventHandler(ControlPlay_MouseLeftButtonDown);   
            tFullScreenButton.MouseLeftButtonDown += new MouseButtonEventHandler(tFullScreenButton_MouseLeftButtonDown);   
            tLoop.MouseLeftButtonDown += new MouseButtonEventHandler(tLoop_MouseLeftButtonDown);   
  
            TstringPlayState.MouseLeftButtonDown += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonDown);   
            TstringPlayState.MouseMove += new MouseEventHandler(TstringPlayState_MouseMove);   
            TstringPlayState.MouseLeftButtonUp += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonUp);   
  
            DownloadedPercent.MouseLeftButtonDown += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonDown);   
            DownloadedPercent.MouseMove += new MouseEventHandler(TstringPlayState_MouseMove);   
            DownloadedPercent.MouseLeftButtonUp += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonUp);  
 
            #region   
  
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);   
  
            for (int i = 1; i <= 20; i++)   
            {   
                Rectangle VolumeItem = (Rectangle) this.FindName(string.Format("_{0}", i));   
                if (VolumeItem != null)   
                {   
                    VolumeItem.MouseEnter += new MouseEventHandler(VolumeItem_MouseEnter);   
                    VolumeItem.MouseLeave += new MouseEventHandler(VolumeItem_MouseEnter);   
                }   
            }  
            #endregion   
  
        }   
  
        void TstringMovieObject_CurrentStateChanged(object sender, RoutedEventArgs e)   
        {   
            switch (this.TstringMovieObject.CurrentState)   
            {   
            case MediaElementState.Buffering:   
            this.Info("Buffering");   
            break;   
            case MediaElementState.Closed:   
            this.Info("Closed");   
            break;   
            case MediaElementState.Opening:   
            this.Info("Opening");   
            break;   
            case MediaElementState.Paused:   
            this.Info("Paused");   
            break;   
            case MediaElementState.Playing:   
            this.Info(null);   
            break;   
            case MediaElementState.Stopped:   
            this.Info("Stopped");   
            break;   
            default:   
            this.Info(null);   
            break;   
            }   
        }   
  
        void tLoop_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)   
        {   
            IsLoop = !IsLoop;   
            tLoop.Opacity = IsLoop ? 0.5 : 1;   
        }   
  
        void TstringMovieObject_MediaEnded(object sender, RoutedEventArgs e)   
        {   
            if (IsLoop)   
            {   
                ControlPlay_MouseLeftButtonDown(sender, null);   
            }   
            tPlayButton.Opacity = 1;   
            ControlPlay.Text = "重播";   
            TstringPlayState.Width = 0;   
            DownloadedPercent.Width = OffsetLoadedProcessBarLeng();   
            TstringMovieObject.Position = new TimeSpan(0, 0, 0);   
            TstringSliderLoadedPercentText.Text = string.Empty;   
        }   
  
        void TstringMovieObject_DownloadProgressChanged(object sender, RoutedEventArgs e)   
        {   
            DownloadedPercent.Width = TstringMovieObject.DownloadProgress * OffsetLoadedProcessBarLeng();   
            TstringSliderLoadedPercentText.Text = string.Format("{0}%", (int) (TstringMovieObject.DownloadProgress * 100));   
        }   
  
        void TstringSliderBg_MouseLeave(object sender, MouseEventArgs e)   
        {   
            IsLeftButtonPressed = false;   
        }   
  
        void TstringPlayState_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)   
        {   
            IsLeftButtonPressed = false;   
        }   
  
        void TstringPlayState_MouseMove(object sender, MouseEventArgs e)   
        {   
            if (IsLeftButtonPressed)   
            {   
                double seconds = this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;   
  
                TstringPlayState.Width = e.GetPosition((UIElement) sender).X;   
                TstringMovieObject.Position = new TimeSpan(0, 0, (int) (seconds / OffsetLeng() * TstringPlayState.Width));   
            }   
        }   
  
        void TstringPlayState_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)   
        {   
            if (!IsLeftButtonPressed)   
            {   
                IsLeftButtonPressed = true;   
                double seconds = this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;   
  
                TstringPlayState.Width = e.GetPosition((UIElement) sender).X;   
                TstringMovieObject.Position = new TimeSpan(0, 0, (int) (seconds / OffsetLeng() * TstringPlayState.Width));   
            }   
        }   
  
        void tFullScreenButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)   
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
            if (Application.Current.Host.Content.IsFullScreen)
            {
                Stage.FullScreen();
            }
            else 
            {
                Stage.UnFullScreen();
            } 
        }   
  
        void tMutedButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)   
        {   
            TstringMovieObject.IsMuted = !TstringMovieObject.IsMuted;   
            tMutedButton.Opacity = TstringMovieObject.IsMuted ? 0.5 : 1;   
        }   
  
        void VolumeSlider_MouseLeave(object sender, MouseEventArgs e)   
        {   
            VolumeSlider.Opacity = 0;   
        }   
  
        void VolumeSlider_MouseEnter(object sender, MouseEventArgs e)   
        {   
            VolumeSlider.Opacity = 1;   
        }   
  
        void Content_FullScreenChanged(object sender, EventArgs e)   
        {   
  
            if (Application.Current.Host.Content.IsFullScreen)   
            {   
                GlobeFrame.Width = Application.Current.Host.Content.ActualWidth;   
                GlobeFrame.Height = Application.Current.Host.Content.ActualHeight;   
            }   
            else  
            {   
                GlobeFrame.Width = 400;   
                GlobeFrame.Height = 300;   
            }   
            DownloadedPercent.Width = this.Width - 60;   
        }   
  
        void VolumeItem_MouseEnter(object sender, MouseEventArgs e)   
        {   
            Rectangle VolumeItem = e.OriginalSource as Rectangle;   
            if (VolumeItem != null)   
            {   
                double _CurrentIndex = double.Parse(VolumeItem.Name.Replace("_", ""));   
                for (int i = 1; i <= 20; i++)   
                {   
                    Rectangle _TempRectangle = (Rectangle) this.FindName("_" + i);   
                    if (_TempRectangle != null)   
                    {   
                        if (i >= _CurrentIndex)   
                        {   
                            _TempRectangle.Opacity = 1;   
                        }   
                        else  
                        {   
                            _TempRectangle.Opacity = 0.2;   
                        }   
                    }   
                }   
  
                this.TstringMovieObject.Volume = 1 - _CurrentIndex / 10;   
            }   
        }   
  
        void TstringMovieObject_MarkerReached(object sender, TimelineMarkerRoutedEventArgs e)   
        {   
            if (this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds > 0)   
            {   
                TstringPlayState.Width = e.Marker.Time.TotalSeconds * OffsetLeng() / this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;   
            }   
        }   
  
        void TstringMovieObject_MediaOpened(object sender, RoutedEventArgs e)   
        {   
            double seconds = this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;   
            for (int i = 1; i < OffsetLeng(); i++)   
            {   
                TimelineMarker marker = new TimelineMarker();   
                double time = seconds / OffsetLeng() * i;   
                marker.Time = new TimeSpan(0, 0, (int) time);   
                marker.Text = time.ToString();   
                marker.Type = "Slider";   
                this.TstringMovieObject.Markers.Add(marker);   
            }   
        }   
  
        void ControlPlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)   
        {   
            switch (this.TstringMovieObject.CurrentState)   
            {   
            case MediaElementState.Paused:   
            this.TstringMovieObject.Play();   
            tPlayButton.Opacity = 1;   
            ControlPlay.Text = "播放";   
            this.Info(string.Empty);   
            break;   
            case MediaElementState.Playing:   
            this.TstringMovieObject.Pause();   
            tPlayButton.Opacity = 0.5;   
            ControlPlay.Text = "暂停";   
            this.Info("暂停");   
            break;   
            case MediaElementState.Stopped:   
            this.TstringMovieObject.Pause();   
            tPlayButton.Opacity = 1;   
            ControlPlay.Text = "播放";   
            break;   
            default:   
            break;   
            }   
        }  
 
        #region Method   
  
        private double OffsetLoadedProcessBarLeng()   
        {   
            return this.Width - 60;   
        }   
  
        private double OffsetLeng()   
        {   
            return this.Width - 140;   
        }   
  
        private void Info(string Information)   
        {   
            if (string.IsNullOrEmpty(Information))   
            {   
                tbInfo.Text = string.Empty;   
            }   
            else  
            {   
                tbInfo.Text = Information;   
            }   
        }  
 
        #endregion   
  
    }   

    
}
